package com.example.evanproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.evanproject.domain.ExamPaperAnswer;
import com.example.evanproject.domain.Exampaper;
import com.example.evanproject.domain.PageView;
import com.example.evanproject.domain.Questions;
import com.example.evanproject.mapper.ExamPaperAnswerMapper;
import com.example.evanproject.mapper.QuestionsMapper;
import com.example.evanproject.service.ExampaperService;
import com.example.evanproject.mapper.ExampaperMapper;


import com.example.evanproject.util.DatesUtils;
import com.example.evanproject.vo.PaperVo;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author hanxiaoqi
* @description 针对表【exampaper(试卷表)】的数据库操作Service实现
* @createDate 2024-03-09 21:52:12
*/
@Service
public class ExampaperServiceImpl extends ServiceImpl<ExampaperMapper, Exampaper>
    implements ExampaperService{


    @Autowired
    private ExampaperMapper exampaperMapper;
    @Autowired
    private QuestionsMapper questionsMapper;
    @Autowired
    private ExamPaperAnswerMapper examPaperAnswerMapper;
    @Autowired
    private PageViewServiceImpl pageViewService;
    @Override
    public Integer saveList(Exampaper exampaper) {
        List<Questions> questionsList=exampaper.getQuestionsList();
        if(questionsList.size()>0){
            List<String> list=new ArrayList<>();
            for (Questions item:questionsList) {
                item.setFlag(String.valueOf(exampaper.getFlag()));
                item.setType(exampaper.getPaperType());
                item.setCreateBy(exampaper.getCreateBy());
                item.setCreatTime(DatesUtils.getTime());
               Integer num= questionsMapper.insert(item);
                list.add(String.valueOf(item.getId()));
            }
            exampaper.setCreateTime(DatesUtils.getTime());
           String str=String.join(",", list);
            exampaper.setContent(str);
        }
        return exampaperMapper.saveList(exampaper);
    }

    @Override
    public Exampaper selectExampaperById(Integer exampaperId) {
        Exampaper exampaper=exampaperMapper.selectInfolist(exampaperId);
        String str=exampaper.getContent();
        if(str!=null){
            String[] list=str.split(",");
            QueryWrapper<Questions> wrapper = new QueryWrapper<Questions>()
                    .select("id", "select_a", "select_b", "select_c","select_d","scores","title")
                    .in("id", list);
           List<Questions> questionsList=questionsMapper.selectList(wrapper);
            exampaper.setQuestionsList(questionsList);
        }
        return exampaper;
    }

    @Override
    public Map<String,Object> submitPaper(PaperVo paperVo) {

//        -----------------------
            /*统计做题数量*/
            Exampaper exampaper=exampaperMapper.selectById(paperVo.getExamPaperId());
            Double sumCourse=0.0;
            Integer countNum=0;
            String errorList="";
            for (Questions item:paperVo.getQuestionsList()) {
                Questions questions=questionsMapper.selectById(item.getId());
                if(item.getRightSelect()==null||item.getRightSelect()==""){
                    errorList=String.join(",",String.valueOf(item.getId()));
                    continue;
                }
                  if(questions.getRightSelect().equals(item.getRightSelect())){
                      sumCourse=sumCourse+ Double.valueOf(questions.getScores());
                      ++countNum;
                  }else {
                      errorList=String.join(",",String.valueOf(item.getId()));
                  }
            }
            ExamPaperAnswer examPaperAnswer=new ExamPaperAnswer();
            BeanUtils.copyProperties(paperVo,examPaperAnswer);
            examPaperAnswer.setErrorList(errorList);
            examPaperAnswer.setQuestionCorrect(countNum);
            examPaperAnswer.setQuestionCount(exampaper.getQuestionCount());
            examPaperAnswer.setAnswerStartTime(paperVo.getAnswerStartTime());
            examPaperAnswer.setAnswerEndTime(DatesUtils.getTime());
            examPaperAnswer.setUserScore(String.valueOf(sumCourse));
        examPaperAnswerMapper.insert(examPaperAnswer);
        Map<String,Object> map=new HashMap<>();
        map.put("score",sumCourse);
        map.put("rigthCount",countNum);
return map;

        }





}




